//新增首营企业(保存)
$("#save")
		.click(
				function() {
					if (judgeInput() == false) {
						$("#save").removeAttr("data-dismiss");
						return;
					}
					$("#save").attr("data-dismiss", "modal");
					var obj = $("#isAddOrUpdate").val();
					if (obj == "add") {
						// 字符串
						var classifyNoStr = "";
						var i = 0;
						$("#selectedCategory").find("tr").each(function() {
							var tdArr = $(this).children();
							var classifyNo = tdArr.eq(0).next().text();
							if (i != 0) {
								classifyNoStr += classifyNo + ";";
							}
							i++;
						});

						if ($("#isVarietyControl").get(0).checked) {
							$("#varietyControl").val(1);
						}
						if ($("#isVarietyControl").get(0).checked == false) {
							$("#varietyControl").val(0);
						}

						if ($("#isSellColdItem").get(0).checked) {
							$("#sellColdItem").val(1);
						}
						if ($("#isSellColdItem").get(0).checked == false) {
							$("#sellColdItem").val(0);
						}
						// 序列化表单为json对象的方法
						$.fn.serializeJson = function() {
							var serializeObj = {};
							var array = this.serializeArray();
							$(array)
									.each(
											function() {
												if (serializeObj[this.name]) {
													if ($
															.isArray(serializeObj[this.name])) {
														serializeObj[this.name]
																.push(this.value);
													} else {
														serializeObj[this.name] = [
																serializeObj[this.name],
																this.value ];
													}
												} else {
													serializeObj[this.name] = this.value;
												}
											});
							return serializeObj;
						};
						// 得到{键:值}这种类型的数据
						var data = $("#firstEnterpriseForm").serializeJson();
						// 然后创建一个对象把表单信息和数组封起来
						var object = {};
						object.firstEnterprise = data;
						object.classifyNoStr = classifyNoStr;
						$
								.ajax({
									type : "POST",
									url : "/Medical_Instrument/firstEnterprise/addFirstEnterprise",
									data : JSON.stringify(object),
									contentType : "application/json;charset=utf-8",
									dataType : "json",
									success : function(data) {
										if (data.result == "success") {
											// 更新成功
											layer.msg('新增成功!', {
												icon : 6,
												time : 5000
											});
											show_table(null, 1);
										}
										if (data.result == "failed") {
											layer.msg('新增失败!', {
												icon : 5,
												time : 1000
											});
										}

									},
									error : function(data) {
										// 请求失败
										layer.msg('请求失败!', {
											icon : 5,
											time : 1000
										});
									}
								});

					} else if (obj = "update") {
						var data = $("#firstEnterpriseForm").serialize();
						$
								.ajax({
									type : "post",
									url : "/Medical_Instrument/firstEnterprise/updateFirstEnterpriseByNo",
									data : data,
									dataType : "json",
									success : function(data) {
										if (data.result != "failed") {
											layer.msg('修改成功!', {
												icon : 6,
												time : 5000
											});
											window.location.reload();
										} else {
											layer.msg('请求失败!', {
												icon : 5,
												time : 1000
											});
										}
										;
									},
									error : function(data) {
										layer.msg('请求失败!', {
											icon : 5,
											time : 1000
										});
									}
								});
					}
				})

function judgeInput() {
	if (JudgefirstNo() == false) {
		return false;
	}
	if ($("#inputDate").val() == "") {
		layer.msg('请填写填表日期!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#linkman").val() == "") {
		layer.msg('请填写企业联系人!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#linkphone").val() == "") {
		layer.msg('请填写企业联系电话!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#enterpriseName").val() == "") {
		layer.msg('请填写企业名称!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#legalPerson").val() == "") {
		layer.msg('请填写企业法定代表人!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#registerAddress").val() == "") {
		layer.msg('请填写企业注册地址!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#enterprisePerson").val() == "") {
		layer.msg('请填写企业负责人!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#warehouseAddress").val() == "") {
		layer.msg('请填写仓库地址!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#qualityDirecter").val() == "") {
		layer.msg('请填写质量负责人!', {
			icon : 5,
			time : 1000
		});
		return false;
	}
	if ($("#bank").val() == "") {
		layer.msg('请填写开户银行!', {
			icon : 5,
			time : 1000
		});
		return false;
	}
	if ($("#bankAccount").val() == "") {
		layer.msg('请填写银行卡号!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	if ($("#organizationNo").val() == "") {
		layer.msg('请填写组织机构代码证!', {
			icon : 5,
			time : 1000
		});
		return false;
	}

	// 委托书有效期 
	var entrustLimitedDate = $("#entrustLimitedDate").val();
	// 组织机构代码证有效期
	var organizationLimitedDate = $("#organizationLimitedDate").val();

	var qualityEndDate = $("#qualityEndDate").val(); // 质保协议起始日期
	var qualityStartDate = $("#qualityStartDate").val(); // 质保协议有效期
	var qualityStartDate = $("#qualityStartDate").val();

	if (getDays(qualityStartDate, qualityStartDate) > 365) {
		layer.msg('质保协议起始日期和有效期相差不得超过一年!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if (getDays($("#inputDate").val(), entrustLimitedDate) > 365) {
		layer.msg('委托书有效期不能超出一年!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if ($("#licenseNo").val() == "") {
		layer.msg('请填写许可证号!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if ($("#recordsNo").val() == "") {
		layer.msg('请填写备案凭证编号!', {
			icon : 5,
			time : 1000
		});
		return;
	}
	if ($("#certificateAuthority").val() == "") {
		layer.msg('请填写发证机关!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if ($("#certificateStartDate").val() == "") {
		layer.msg('请填写发证日期!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	if ($("#certificateEndDate").val() == "") {
		layer.msg('请填写发证有效期!', {
			icon : 5,
			time : 1000
		});
		return;
	}

	return true;
}

function formatBankNo(BankNo) {
	if (BankNo.value == "")
		return;
	var account = new String(BankNo.value);
	account = account.substring(0, 22); /* 帐号的总数, 包括空格在内 */
	if (account.match(".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null) {
		/* 对照格式 */
		if (account.match(".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|"
				+ ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|"
				+ ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}|"
				+ ".[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{7}") == null) {
			var accountNumeric = accountChar = "", i;
			for (i = 0; i < account.length; i++) {
				accountChar = account.substr(i, 1);
				if (!isNaN(accountChar) && (accountChar != " "))
					accountNumeric = accountNumeric + accountChar;
			}
			account = "";
			for (i = 0; i < accountNumeric.length; i++) { /* 可将以下空格改为-,效果也不错 */
				if (i == 4)
					account = account + " "; /* 帐号第四位数后加空格 */
				if (i == 8)
					account = account + " "; /* 帐号第八位数后加空格 */
				if (i == 12)
					account = account + " ";/* 帐号第十二位后数后加空格 */
				account = account + accountNumeric.substr(i, 1)
			}
		}
	} else {
		account = " " + account.substring(1, 5) + " "
				+ account.substring(6, 10) + " " + account.substring(14, 18)
				+ "-" + account.substring(18, 25);
	}
	if (account != BankNo.value)
		BankNo.value = account;
}

// 返回日期相差多少天
function getDays(strDateStart, strDateEnd) {
	var strSeparator = "-"; // 日期分隔符
	var oDate1;
	var oDate2;
	var iDays;
	oDate1 = strDateStart.split(strSeparator);
	oDate2 = strDateEnd.split(strSeparator);
	var strDateS = new Date(oDate1[0], oDate1[1] - 1, oDate1[2]);
	var strDateE = new Date(oDate2[0], oDate2[1] - 1, oDate2[2]);
	iDays = parseInt(Math.abs(strDateS - strDateE) / 1000 / 60 / 60 / 24)// 把相差的毫秒数转换为天数
	return iDays;
}

function buttonStatus(string) {
	if (string == "新增") {
		document.getElementById("firstEnterpriseForm").reset();
		attr(false);
		attrAuditButton();
		$("#businessDept").css("display", "none");
		$("#qualityAdmin").css("display", "none");
		$("#qualityDept").css("display", "none");
		$("#qualityPeople").css("display", "none");
		$("#withdraw").css("display", "none");
		$("#save").css("display", "inline-block");
		$("#set").attr("data-target", "#setting");
		$("#onlineEnquiry").css("display", "none");
		$("#qualityPeopleConclusion").attr("disabled", "disabled");
		$("#businessDepOpinion").attr("disabled", "disabled");
		$("#businessAuditDate").attr("disabled", "disabled");
		$("#qualityAdminConclusion").attr("disabled", "disabled");
		$("#qualityDeptOpinion").attr("disabled", "disabled");
		$("#qualityPeopleOpinion").attr("disabled", "disabled");

		var a = document.getElementById("inputDate");
		var b = new Date();
		a.value = b.format('yyyy-MM-dd');
	}

	if (string == "查看") {
		$("#businessDept").css("display", "none");
		$("#qualityAdmin").css("display", "none");
		$("#qualityDept").css("display", "none");
		$("#qualityPeople").css("display", "none");
		$("#withdraw").css("display", "none");
		$("#save").css("display", "inline-block");
	}
	if (string == "业务部") {
		$("#qualityAdmin").css("display", "none");
		$("#qualityPeople").css("display", "none");
		$("#qualityDept").css("display", "none");
		$("#save").css("display", "none");
		$("#withdraw").css("display", "none");
		$("#businessDept").css("display", "inline-block");
		getEnterpriseInfo("#businessSign", "#firstEnterpriseForm");
		$("#operatorPassword").val("");
	}
	if (string == "质量管理员") {
		$("#businessDept").css("display", "none");
		$("#qualityPeople").css("display", "none");
		$("#qualityDept").css("display", "none");
		$("#save").css("display", "none");
		$("#qualityAdmin").css("display", "inline-block");
		$("#withdraw").css("display", "inline-block");
		$("#password_QA").val("");
		getEnterpriseInfo("#qualityAdminSign", "#firstEnterpriseForm");
	}
	if (string == "质量管理部") {
		$("#businessDept").css("display", "none");
		$("#qualityAdmin").css("display", "none");
		$("#qualityPeople").css("display", "none");
		$("#save").css("display", "none");
		$("#withdraw").css("display", "inline-block");
		$("#qualityDept").css("display", "inline-block");
		$("#operatorName_QD").val("");
		getEnterpriseInfo("#qualityDeptSign", "#firstEnterpriseForm");
	}
	if (string == "质量负责人") {
		$("#businessDept").css("display", "none");
		$("#qualityAdmin").css("display", "none");
		$("#qualityDept").css("display", "none");
		$("#withdraw").css("display", "inline-block");
		$("#qualityPeople").css("display", "inline-block");
		$("#save").css("display", "none");
		$("#password_QP").val("");
		getEnterpriseInfo("#qualityPeopleSign", "#firstEnterpriseForm");
	}
	if (string == "转为供货方") {
		getEnterpriseInfo("#waitForProvider", "#providerInfoForm");
	}

}

// 获取选中的企业信息
function getEnterpriseInfo(signInfo, formName) {
	var supplier_enterprise_no = getSelectedNo("qualityDeptSign");
	if (supplier_enterprise_no == "" || supplier_enterprise_no == null) {
		$(signInfo).removeAttr("data-target");
		return;
	}
	if (formName == "#firstEnterpriseForm")
		$(signInfo).attr("data-target", "#addbusiness");
	else
		$(signInfo).attr("data-target", "#waitForProviderModal");
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/firstEnterprise/getFirstEnterpriseByNo",
		data : {
			"supplier_enterprise_no" : supplier_enterprise_no
		},
		dataType : "json",
		success : function(data) {
			if (data.result != "failed") {
				$(formName).populateForm(formatDate(data.result));
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});
	attr(true);
}

// 经营类型下拉框变化
$("#manageType").change(function() {
	var manage_type = $(this).children("option:selected").val();
	if (manage_type == 1) {
		// 经营企业
		$("#businessScope").css("display", "block");

		$("#date01").css("top", "645px");
		$("#date02").css("top", "675px");
		$("#date03").css("top", "705px");
		$("#date04").css("top", "735px");

	} else if (manage_type == 2) {
		// 生产企业
		$("#businessScope").css("display", "none");

		$("#date01").css("top", "543px");
		$("#date02").css("top", "573px");
		$("#date03").css("top", "603px");
		$("#date04").css("top", "633px");
	}

});

JudgefirstNo = function() {
	var firstNo = $("#firstNo").val();
	if (parseInt(firstNo) < 1000) {
		layer.msg("编号的格式为大于1000的四位数", {
			icon : 5,
			time : 2000
		});
		$("#firstNo").focus();
		return false;
	}
	$.ajax({
		type : "post",
		url : "/Medical_Instrument/firstEnterprise/JudgefirstNo",
		data : {
			"firstNo" : firstNo
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "true") {
				layer.msg("编号为" + firstNo + "的企业已存在，请重新输入", {
					icon : 5,
					time : 2000
				});
				$("#firstNo").focus();
				return false;
			}
			$("#supplierEnterpriseNo").val(firstNo);
			return true;
		},
		error : function(data) {
			layer.msg('发生异常!请稍后再试或联系管理员', {
				icon : 5,
				time : 1000
			});
		}
	});

}